/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 北京银杉金服科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2019/1/13 19:53
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.server.context;

import com.alibaba.fastjson.JSON;
import com.hyacinth.server.action.request.AuroraRequest;
import com.hyacinth.server.action.response.AuroraResponse;
import com.hyacinth.server.action.response.Wrapper;
import com.hyacinth.server.constant.Constant;
import com.hyacinth.server.thread.ThreadLocalHolder;

/**
 * 应用上下文
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public final class AuroraContext {

    /**
     * current thread request
     */
    private AuroraRequest request ;

    /**
     * current thread response
     */
    private AuroraResponse response ;

    public AuroraContext(AuroraRequest request, AuroraResponse response) {
        this.request = request;
        this.response = response;
    }


    public static AuroraRequest getRequest(){
        return AuroraContext.getContext().request ;
    }

    public static AuroraResponse getResponse(){
        return AuroraContext.getContext().response;
    }

    public static void setContext(AuroraContext context){
        ThreadLocalHolder.setAuroraContext(context);
    }


    public static void removeContext(){
        ThreadLocalHolder.removeAuroraContext();
    }

    public static AuroraContext getContext(){
        return ThreadLocalHolder.getAuroraContext();
    }

    /**
     * response json message
     * @param wrapper
     */
    public void json(Wrapper wrapper){
        AuroraContext.getResponse().setContentType(Constant.ContentType.JSON);
        AuroraContext.getResponse().setHttpContent(JSON.toJSONString(wrapper));
    }

    /**
     * response text message
     * @param text response body
     */
    public void text(String text){
        AuroraContext.getResponse().setContentType(Constant.ContentType.TEXT);
        AuroraContext.getResponse().setHttpContent(text);
    }

    /**
     * response html
     * @param html response body
     */
    public void html(String html){
        AuroraContext.getResponse().setContentType(Constant.ContentType.HTML);
        AuroraContext.getResponse().setHttpContent(html);
    }
}
